<?php
class MonthamgianhomsController extends AppController {

	var $name = 'Monthamgianhoms';
	var $uses = array('Monthamgianhom','Khoa','Nganh','Ctdt','NhomHp');
	
	function index() {
		$this->Monthamgianhom->recursive = 0;
		$this->set('monthamgianhoms', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid monthamgianhom', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('monthamgianhom', $this->Monthamgianhom->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Monthamgianhom->create();
			if ($this->Monthamgianhom->save($this->data)) {
				$this->Session->setFlash(__('The monthamgianhom has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The monthamgianhom could not be saved. Please, try again.', true));
			}
		}
		$mons = $this->Monthamgianhom->Mon->find('list');
		$nhomHps = $this->Monthamgianhom->NhomHp->find('list');
		$this->set(compact('mons', 'nhomHps'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid monthamgianhom', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Monthamgianhom->save($this->data)) {
				$this->Session->setFlash(__('The monthamgianhom has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The monthamgianhom could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Monthamgianhom->read(null, $id);
		}
		$mons = $this->Monthamgianhom->Mon->find('list');
		$nhomHps = $this->Monthamgianhom->NhomHp->find('list');
		$this->set(compact('mons', 'nhomHps'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for monthamgianhom', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Monthamgianhom->delete($id)) {
			$this->Session->setFlash(__('Monthamgianhom deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Monthamgianhom was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function xemctdt()
	{
		$khoas = $this->Khoa->find('list',array('fields'=>array('tenkhoa')));
		$khoahoc = $this->Ctdt->find('list',array('fields'=>array('khoa','khoa'),'group'=>array('khoa')));
		$nganhs = $this->Nganh->find('list',array('fields'=>array('tenNganh')));
		$this->set(compact('khoas','khoahoc','nganhs'));
		
	}
	
	function capnhatnganh($makhoa = null)
	{
		$this->layout = 'ajax';
		$nganhs = $this->Nganh->find('list',array('conditions'=>array('khoa_id'=>$makhoa),'fields'=>array('tenNganh')));
		$this->set(compact('nganhs'));
	}

	function capnhatmon($manganh = null,$khoa = null,$makhoa=null)
	{
		$this->layout = 'ajax';
		if($manganh==0)
		{
			$nganhs = $this->Nganh->find('list',array('conditions'=>array('khoa_id'=>$makhoa),'fields'=>array('tenNganh')));
			$manganh = key($nganhs);
		}
		$ctdt = $this->Ctdt->find('list',array('conditions'=>array('Khoa'=>$khoa,'nganh_id'=>$manganh)));
		$nhomhp = $this->NhomHp->find('all',array('conditions'=>array('NhomHp.ctdt_id'=>$ctdt)));
		$nhp = $this->NhomHp->find('list',array('conditions'=>array('ctdt_id'=>$ctdt)));
		$mons = $this->Monthamgianhom->find('all',array('conditions'=>array('Monthamgianhom.nhomhp_id'=>$nhp)));
		$this->set(compact('nhomhp','mons'));
	}

}
